home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part2 / 11977 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  1.6 KB

  1. Path: ix.netcom.com!news
  2. From: miker3@ix.netcom.com (Mike Rubenstein)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: extern consts
  5. Date: Sun, 17 Mar 1996 14:26:40 GMT
  6. Organization: Netcom
  7. Message-ID: <314c2077.138956468@nntp.ix.netcom.com>
  8. References: <4idbcv$ue2@news7.erols.com>
  9. NNTP-Posting-Host: ix-dc12-26.ix.netcom.com
  10. X-NETCOM-Date: Sun Mar 17  8:29:31 AM CST 1996
  11. X-Newsreader: Forte Agent .99d/32.182
  12.  
  13. ccobb <ccobb@erols.com> wrote:
  14.  
  15. > The following code works on one C++ compiler but does not work on two 
  16. > others I have tried.  Can anyone give me an authoritive answer as to 
  17. > whether this is legal C++?
  18. > --- xxx.h ---
  19. > extern const int MYCONST;
  20. > --- xxx.cc ---
  21. > #include "xxx.h"
  22. > const int MYCONST = 11;
  23. > --- yyy.cc ---
  24. > #include <iostream.h>
  25. > #include "xxx.h"
  26. > main()
  27. > {
  28. >    char myarray[MYCONST];
  29. >    cout << "MYCONST is " << MYCONST << endl;
  30. >    cout << "sizeof myarray[] is " << sizeof(myarray) << endl;
  31. >    return 0;
  32. > }
  33. > --- end ---
  34. > The whole point here is whether consts can be extern.  Arrays require 
  35. > constant expressions.  Techincally, the array size is a constant because 
  36. > it is declared const.  However, the value of the constant is set in 
  37. > another file.  This means that you could change your constants without 
  38. > having to recompile!  If you think the above is trivial you are missing 
  39. > the point.  The above is not possible in C nor in two of the three C++ 
  40. > compilers I have tried it on.
  41. > Any comments?
  42.  
  43. It's not legal.  extern const is legal, but in any compilation unit
  44. that does not see the initializer the const variable is not a constant
  45. expression.
  46.  
  47. Michael M Rubenstein
  48.